package sa.com.sure.ejtimaa.database;


import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.model.MeetingCancelationReasonInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MeetingCancelationReasons {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MeetingCancelationReasons";
	
    public static final String MeetingIDField = "MeetingID"; 
    public static final String CancelationReasonField = "CancelationReason";
    
    private String[] allColumns = { MeetingIDField, CancelationReasonField};
    
    Cursor cursor = null;
    
    public MeetingCancelationReasons(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static MeetingCancelationReasons Instance = new MeetingCancelationReasons(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public MeetingCancelationReasonInfo GetItem(int MeetingID) throws SQLException
	{	
		try	
		{
			open();
		
			MeetingCancelationReasonInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingIDField+ "=" + MeetingID ,null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public void addItem(MeetingCancelationReasonInfo item) 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(MeetingIDField, item.GetMeetingID());  
	        values.put(CancelationReasonField, item.GetCancelationReason());  
	        
	        dbHelper.db.insert(TABLE_Name, null, values); 
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
	
	 public boolean updateItem(MeetingCancelationReasonInfo item) 
	 {
		 try	
			{		 
			 	open();
			 	
			    ContentValues values = new ContentValues();		    
			    
		        values.put(CancelationReasonField, item.GetCancelationReason()); 
			    
			    return dbHelper.db.update(TABLE_Name, values, MeetingIDField + "=" + item.GetMeetingID(), null) > 0;			    			   
			    			    	        
			}
		    finally{        	
		        close();
		    }  	               
	 }
	
	
    private MeetingCancelationReasonInfo getItemInfo(Cursor cursor) {  
    	
    	MeetingCancelationReasonInfo itemInfo = new MeetingCancelationReasonInfo();
    	
    	itemInfo.SetMeetingID(cursor.getInt(cursor.getColumnIndex(MeetingIDField)));    	
    	itemInfo.SetCancelationReason(cursor.getString(cursor.getColumnIndex(CancelationReasonField)));    	
    	
		return itemInfo;
	}
}
